package com.qfedu.video.controller;

import com.qfedu.video.entry.Course;
import com.qfedu.video.entry.Subject;
import com.qfedu.video.entry.Video;
import com.qfedu.video.service.CourseService;
import com.qfedu.video.service.SubjectService;
import com.qfedu.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author yanzhenwei
 * @date 2021-07-14 10:41
 */
@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    CourseService courseService;

    @Autowired
    SubjectService subjectService;

    @Autowired
    VideoService videoService;

    @RequestMapping("/list")
    public String showList(int subjectId, Model model){


        Subject subject = subjectService.getSubjectById(subjectId);
        //根据学科id查找course
        List<Course> courseList =courseService.getCourseBySubjectId(subjectId);



        for (Course course:courseList) {
             int courseId = course.getId();//课程的id
             List<Video> videoList =videoService.getVideoListByCourseId(courseId);
             course.setVideoList(videoList);
        }

        subject.setCourseList(courseList);

        model.addAttribute("subject",subject);

       return   "before/course";
    }

    @RequestMapping("/list2")
    @ResponseBody
    public List<Course> showList2(){
        return   courseService.getListByCondition();
    }

    @RequestMapping("/insert")
    @ResponseBody
    public void insert(Course course){
        courseService.insertCourse(course);
    }
}
